const db = require('../db/index');


// 获取班级名称

exports.getClass = (req, res) => {
        const sql = 'select class_name from  class_name;'
        db.query(sql, (err, results) => {
            if (err) return res.cc(err)
            if (results.length === 0) return res.cc('查询失败')
            res.send({
                status: 200,
                msg: '查询成功',
                data: results,
            })
        })
    }
    // 按班级查看
exports.classView = (req, res) => {
        const userinfo = req.body
        const sql = 'select * from studentmsg,class_name where studentmsg.class_id=class_name.id and class_name=?'
        db.query(sql, [userinfo.classname], (err, results) => {
            if (err) return res.cc(err)
            if (results.length === 0) return res.cc('查询失败')
                // 不能看密码
                // const user = {...results, student_pwd: '' }
            res.send({
                status: 200,
                msg: '查询成功',
                data: results,
            })
        })
    }
    // 按班级查看考勤
exports.classCheck = (req, res) => {
    const userinfo = req.body
    const sql = 'select name, sex, class, age, student_id, email from studentmsg,class_name where studentmsg.class_id=class_name.id and  class_name=? and studentmsg.`Where the attendendance`=true;'
    db.query(sql, [userinfo.classname], (err, results) => {
        if (err) return res.cc(err)
        if (results.length === 0) return res.cc('查询失败')
        res.send({
            status: 200,
            msg: '查询成功',
            data: results,
        })
    })
}


// 获取系名称

exports.getmajor = (req, res) => {
        const sql = 'select major_name from  major;'
        db.query(sql, (err, results) => {
            if (err) return res.cc(err)
            if (results.length === 0) return res.cc('查询失败')
            res.send({
                status: 200,
                msg: '查询成功',
                data: results,
            })
        })
    }
    // 按系查看
exports.majorView = (req, res) => {
    const userinfo = req.body

    const sql = 'select * from studentmsg,major where studentmsg.major_id=major.id and major_name = ?;'
    db.query(sql, [userinfo.majorname], (err, results) => {
        if (err) return res.cc(err)
        if (results.length === 0) return res.cc('查询失败')
        res.send({
            status: 200,
            msg: '查询成功',
            data: results,
        })
    })
}


// 按系查看考勤
exports.majorCheck = (req, res) => {
    const userinfo = req.body
    const sql = 'select name, sex, class, age, student_id, email from studentmsg,major where studentmsg.major_id=major.id and major_name = ? and studentmsg.`Where the attendendance`=true;'
    db.query(sql, [userinfo.majorname], (err, results) => {
        if (err) return res.cc(err)
        if (results.length === 0) return res.cc('查询失败')
        res.send({
            status: 200,
            msg: '查询成功',
            data: results,
        })
    })
}


exports.getAttendance = (req, res) => {
    const userinfo = req.body
    const sql = 'select * from studentmsg where student_id=?;'
    db.query(sql, [userinfo.student_id], (err, results) => {
        if (err) return res.cc(err)
        if (results.length === 0) return res.cc('查询失败')
        res.send({
            status: 200,
            msg: '查询成功',
            data: results,
        })
    })
}